package edu.tongji.engine.db.dao;

import java.util.List;
import java.util.Set;

import org.hibernate.Criteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Restrictions;

import edu.tongji.engine.common.Log;
import edu.tongji.engine.db.HibernateTemplate;
import edu.tongji.engine.def.Node;
import edu.tongji.engine.exe.ProcessInstance;
import edu.tongji.engine.exe.Token;

public class TokenDao implements ITokenDao{

	private HibernateTemplate session;
	private ProcessDao processDao;

	public TokenDao(){
		session = new HibernateTemplate();
		processDao = new ProcessDao();
	}
	public boolean deleteToken(Token token) {
		// TODO Auto-generated method stub
		try{
//			token.setPi(null);
//			session.saveOrUpdate(token);
//			Token deleteToken = this.getTokenById(token.getId());
//			session.delete(deleteToken);
//			String hqlDelete = "delete Token t where t.id = :tokenId ";
//				int deletedEntities = session.getSession().createQuery( hqlDelete )
//				    .setLong("tokenId", token.getId())
//				    .executeUpdate();
			String hql = "delete Token t where t.id = ? ";
			session.deleteById(hql, token.getId());

			return true;
			}
			catch(Exception e){
				System.out.println(e.toString());
				Log.getLoger().loger.error("delete Token Object Fail! ", e);
				return false;
			}
	}

	public Set<Token> getProcessChildTokens(String processInstanceName) {
		// TODO Auto-generated method stub
		try{
			//Token token = new Token();
			ProcessInstance pi = processDao.getProcessInstance(processInstanceName);

			return pi.getTokens();
			}
			catch(Exception e){
				System.out.println(e.toString());
				Log.getLoger().loger.error("get Token Object Fail! ", e);
				return null;
			}
	}

	public Token getProcessToken(String processInstanceName) {
		// TODO Auto-generated method stub
		Set<Token> tokens = this.getProcessChildTokens(processInstanceName);
		return tokens.iterator().next();
	}

	public Node getTokenNode(Token token) {
		// TODO Auto-generated method stub

		return null;
	}

	public boolean saveToken(Token token) {
		try{
			session.saveOrUpdate(token);
			return true;
			}
			catch(Exception e){
				System.out.println(e.toString());
				Log.getLoger().loger.error("save Token Object Fail! ", e);
				return false;
			}
	}
	//get Tokens on the same node with this token
	public List<Token> getNodeTokens(Token token){

		try{
			Token  tmpToken = new Token();
			System.out.println(token.getPi().getId());
			System.out.println(token.getNode().getId());

			//tmpToken.setNode(token.getNode());
			//tmpToken.setPi(token.getPi());
			Criteria criteria = session.getSession().createCriteria(Token.class);

			criteria.createCriteria("node","n").add(Expression.eq("n.id", token.getNode().getId()));
			criteria.createCriteria("pi", "p").add(Expression.eq("p.id", token.getPi().getId()));

			return criteria.list();
			//return session.findByCriteriaExample(Token.class, Example.create(tmpToken));
			}
			catch(Exception e){
				System.out.println(e.toString());
				Log.getLoger().loger.error("get node Token Object Fail! ", e);
				return null;
			}

	}
	public Token getTokenById(Long id) {
		// TODO Auto-generated method stub
		try{
//			Token token = new Token();
//			token.setId(id);
//			Example example = Example.create(token);
//			return (Token)session.findByCriteriaExample(Token.class, example).get(0);

			List tokens = session.getSession().createCriteria(Token.class)
		    .add( Restrictions.eq("id", id)).list();
			return (Token)tokens.get(0);

		}
			catch(Exception e){
				System.out.println(e.toString());
				Log.getLoger().loger.error("get Token Object Fail! ", e);
				return null;
			}
	}
	public boolean deleteAllTokens() {
		try{
			String hql = "delete from Token";
			session.deleteByHQL(hql);
			return true;
			}
			catch(Exception e){
				System.out.println(e.toString());
				Log.getLoger().loger.error("delete all Objects Fail! ", e);
				return false;
			}
	}

}
